using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using HappySprite;
using HappySprite.Simulation;

namespace HappyInvaders
{
    class SaucerEntity : Entity
    {
        public SaucerEntity()
        {
            Item.CollisionShape = Box.CreateFromSize(Constants.SaucerWidth, Constants.SaucerHeight);
            Item.CollisionGroup = Constants.SaucerGroup;
            Item.CollisionMask = Constants.BulletGroup;
 
            Item.SetWorldLimits(Constants.SaucerMinX, 0f, Constants.SaucerMaxX, Constants.WorldHeight);
            Item.Transform.PositionY = Constants.SaucerY;

            // Handle world limits
            Item.WorldLimitsExceeded += Remove;

            // Handle collision
            Item.Collision += delegate(CollisionEventArgs e)
            {
                Remove();

                // Apply score
                int rnd = Global.Random.Next(4);

                switch (rnd)
                {
                    case 0: Owner.Score += 50; break;
                    case 1: Owner.Score += 100; break;
                    case 2: Owner.Score += 150; break;
                    default: Owner.Score += 300; break;
                }
            };
        }

        internal override void OnAdd()
        {
            Owner.SaucerCount++;
        }

        internal override void OnRemove()
        {
            Owner.SaucerCount--;
            Owner.ScheduleSpawnSaucer();
        }

        public void StartLeft()
        {
            Item.Transform.PositionX = Constants.SaucerMinX + Constants.SaucerWidth + 1f;
            Item.VelocityX = Constants.SaucerSpeed;
        }

        public void StartRight()
        {
            Item.Transform.PositionX = Constants.SaucerMaxX - Constants.SaucerWidth - 1f;
            Item.VelocityX = -Constants.SaucerSpeed;
        }
    }
}
